#include "src/Terrain.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

Terrain * initTerrain(Terrain * Ter)
{
    Ter = (Terrain *)malloc(sizeof(Terrain));

char premier_Terrain[15][20] = {
 "~*******************",
		"~...................",
		"~...#######.........",
		"~.........###.......",
		"~...................",
		"~...####............",
		"~......#..++++......",
		"~......#............",
		"~......#..##++#..+..",
		"~......#..#..#....#.",
		"~......#..#.......#.",
		"~......#..#.......#.",
		"~.................#.",
		"~...................",
        "~*******************"
};
    Ter->dimY = 15;
    Ter->dimX = 20;

    int i, j;

    Ter->tab = (char **)malloc(sizeof(char *)*Ter->dimY);
    	for (j=0; j<Ter->dimY; j++)

		Ter->tab[j] = (char *)malloc(sizeof(char)*Ter->dimX);

	for(j=0;j<Ter->dimY;++j)
		for(i=0;i<Ter->dimX;++i)
			Ter->tab[j][i] = premier_Terrain[j][i];







			printf(" %d \n", Ter->dimX);
			printf(" %d \n", Ter->dimY);

			return Ter;

}
Terrain * intiTerrain1 (Terrain * Ter)
{
    Ter = (Terrain *)malloc(sizeof(Terrain));

char Terro[15][5] = {
 "*****",
 ".....",
 ".....",
 ".....",
 ".####",
 ".##..",
 "..#..",
 "..###",
 "...#.",
 "...#.",
 ".#...",
 ".###.",
 ".....",
 ".....",
 "*****"
};
    Ter->dimY = 15;
    Ter->dimX = 5;

    int i, j;

    Ter->tab = (char **)malloc(sizeof(char *)*Ter->dimY);
    printf("là 2-2-2\n");
    	for (j=0; j<Ter->dimY; j++)

		Ter->tab[j] = (char *)malloc(sizeof(char)*Ter->dimX);

	for(j=0;j<Ter->dimY;++j)
		for(i=0;i<Ter->dimX;++i)
			Ter->tab[j][i] = Terro[j][i];

			return Ter;

}

Terrain * intiTerrain2 (Terrain * Ter)
{
    Ter = (Terrain *)malloc(sizeof(Terrain));

char Terro[15][5] = {
 "*****",
 ".#...",
 ".#..#",
 ".+..+",
 "....#",
 "..###",
 ".....",
 "..#..",
 "..#..",
 "..#..",
 ".++..",
 ".....",
 ".##..",
 ".....",
 "*****"
};
    Ter->dimY = 15;
    Ter->dimX = 5;

    int i, j;

    Ter->tab = (char **)malloc(sizeof(char *)*Ter->dimY);
    printf("là 2-2-3\n");
    	for (j=0; j<Ter->dimY; j++)

		Ter->tab[j] = (char *)malloc(sizeof(char)*Ter->dimX);

	for(j=0;j<Ter->dimY;++j)
		for(i=0;i<Ter->dimX;++i)
			Ter->tab[j][i] = Terro[j][i];

			return Ter;

}

Terrain * intiTerrain3 (Terrain * Ter)
{
    Ter = (Terrain *)malloc(sizeof(Terrain));

char Terro[15][5] = {
"*****",
".....",
"..+++",
".....",
".#...",
".#..#",
"...##",
".....",
"+#...",
"#+...",
".....",
".....",
".##..",
".#...",
"*****"
};
    Ter->dimY = 15;
    Ter->dimX = 5;

    int i, j;

    Ter->tab = (char **)malloc(sizeof(char *)*Ter->dimY);
    printf("là 2-2-4\n");
     printf("%d", 8);
    	for (j=0; j<Ter->dimY; j++)

		Ter->tab[j] = (char *)malloc(sizeof(char)*Ter->dimX);

	for(j=0;j<Ter->dimY;++j)
		for(i=0;i<Ter->dimX;++i)
			Ter->tab[j][i] = Terro[j][i];

			return Ter;

}

Terrain * intiTerrain4 (Terrain * Ter)
{
    Ter = (Terrain *)malloc(sizeof(Terrain));

char Terro[15][5] = {
 "*****",
 ".....",
 "...++",
 ".....",
 ".....",
 "...##",
 ".....",
 "..#..",
 ".....",
 "..+..",
 ".....",
 ".....",
 "++##.",
 ".....",
 "*****"
};
    Ter->dimY = 15;
    Ter->dimX = 5;

    int i, j;

    Ter->tab = (char **)malloc(sizeof(char *)*Ter->dimY);
    printf("là 2-2-5\n");

    	for (j=0; j<Ter->dimY; j++)
            Ter->tab[j] = (char *)malloc(sizeof(char)*Ter->dimX);

	for(j=0;j<Ter->dimY;++j)
		{
		    for(i=0;i<Ter->dimX;++i)
			{
            Ter->tab[j][i] = Terro[j][i];
			printf("%c",Ter->tab[j][i]) ;
			}


		}
			return Ter;

}

void libereTerrain(Terrain * Ter)
{
    Ter->dimX=0;
    Ter->dimY=0;
    Ter->tab = NULL;
}

//rajouter le cas pour les zombies
int deplacementPersoValide(const int x, const int y,const Terrain * Ter)
{
	if (x>=0 && x<Ter->dimX && y>=0 && y<Ter->dimY && Ter->tab[y][x]!='#' &&Ter->tab[y][x]!='@'&& Ter->tab[y][x]!='*' && Ter->tab[y][x]!='+')
		return 1;
	else
		return 0;
}

const char terrainGetCharCoord (const Terrain *Ter, int x,int y)
{
   /* if (x< Ter->dimX && y<Ter->dimY)
    {
        printf("%d",56);
    }*/

    //assert(x<= Ter->dimX && y<=Ter->dimY);
    return Ter->tab[y][x];
}

// modifie le caractère d'un terrain celon son ordonnée et son absices
void terrainSetChatCoord(Terrain * Ter, int x, int y, char a)
{
    Ter->tab[x][y]=a;
}

// rajouter le cas avec les zombies
int deplacementProjectileValide (Terrain * Ter, int x, int y)
{
    if (x>0 && x<Ter->dimX && y>=0 && y<Ter->dimY && Ter->tab[y][x]!='#' && Ter->tab[y][x]!='~'&& Ter->tab[y][x]!='*' && Ter->tab[y][x]!='+')
		return 1;
	else
		return 0;
}

// rajouter le cas avec les persos/projectile
int deplacementZombieValide (Terrain * Ter, int x, int y)
{
    if (x>=0 && x<Ter->dimX && y>=0 && y<Ter->dimY && (Ter->tab[y][x]!='#') && Ter->tab[y][x]!='*'  && Ter->tab[y][x]!='+' )
		return 1;
	else
		return 0;
}

// permet d'obtenir la colonne d'un tableau à partir du numéro de celle ci
char * getcolonne (Terrain * ter, int a)
{
    assert (a <= ter->dimX);
    char * colonne;
    colonne = malloc(ter->dimY*sizeof(Terrain));
    int compteur;
    for (compteur = 0 ; compteur < ter->dimY ; compteur ++)
    {
        colonne[compteur] = ter->tab[compteur][a];
    }
    return colonne;
}

int getDimX (const Terrain * Ter)

{
    return Ter->dimX;
}

int getDimY (const Terrain * Ter)
{
    return Ter->dimY;
}
